//
//  Unit->m
//  MapEdit
//
//  Created by Steven Canfield on 6/27/08->
//  Copyright 2008 __MyCompanyName__-> All rights reserved->
//

#import "Unit.h"


@implementation Unit

- (BOOL)isEqual:(id)object {
	if([object class] == [self class]) {
		return [self identifier] == [(Unit*)object identifier];
	}
	return NO;
}

/* Initializer */
- (id)initWithData:( chk_unit_t * )u {
	if((self = [super init])) {
		self->unit = u;
		frameNumber = 0;
	}
	return self;
}
+ (Unit *)unitWithData:(chk_unit_t *)u {
	return [[((Unit*)[self alloc]) initWithData:u] autorelease];
}


/* Identifier ( Unit Serial Number ) */
- (u_int32_t)identifier {
	return unit->identifier;
}

/* Unit Location */
- (NSPoint)location {
	return NSMakePoint( unit->x, unit->y );
}
- (void)setLocation:(NSPoint)point {
	unit->x = point.x;
	unit->y = point.y;
}

/* Unit Type */
- (u_int16_t)type {
	return unit->type;
}
- (void)setType:(u_int16_t)type {
	NSLog(@"Setting Type");
	unit->type = type;
}

/* Unit Owner */
- (u_int8_t)owner {
	return unit->owner;
}
- (void)setOwner:(u_int8_t)owner {
	unit->owner = owner;
}

/* Health / Shields Energy */
- (u_int8_t)health {
	return unit->health;
}
- (void)setHealth:(u_int8_t)health {
	unit->health = health;
}

- (u_int8_t)maxHealth {
	return 100;
}

- (BOOL)healthValid {
	return unit->stat_valid & 2;
}

- (u_int8_t)shield {
	return unit->shield;
}
- (void)setShield:(u_int8_t)shield {
	unit->shield = shield;
}
- (u_int8_t)maxShield {
	return 100;
}
- (BOOL)shieldValid {
	return unit->stat_valid & 4;
}

- (u_int8_t)energy {
	return unit->energy;
}
- (void)setEnergy:(u_int8_t)energy {
	unit->energy = energy;
}
- (u_int8_t)maxEnergy {
	return 100;
}
- (BOOL)energyValid { 
	return unit->stat_valid & 8;
}

- (BOOL)resourceValid {
	return unit->stat_valid & 16;
}

/* Resources */
- (u_int32_t)resource {
	return unit->resource;
}

- (void)setResource:(u_int32_t)resource {
	unit->resource = resource;	
}

- (BOOL)cloaked {
	return (unit->property & 1);
}
- (void)setCloaked:(BOOL)flag {
	unit->property ^= 1;
}

- (BOOL)cloakedValid {
	return ( unit->property_valid & 1 );
}

- (BOOL)burrowed {
	return unit->property & 2;
}
- (void)setBurrowed:(BOOL)flag {
	unit->property ^= 2;
}

- (BOOL)burrowedValid {
	return unit->property_valid & 2;
}


- (BOOL)hallucinated {
	return unit->property & 8;
}
- (void)setHallucinated:(BOOL)flag {
	unit->property ^= 8;
}
- (BOOL)hallucinatedValid {
	return unit->property_valid & 8;
}

- (BOOL)invincible {
	return unit->property & 16;
}
- (void)setInvincible:(BOOL)flag {
	unit->property ^= 16;
}
- (BOOL)invincibleValid {
	return unit->property_valid & 16;	
}

- (int)frameNumber {
	return frameNumber;
}

- (void)setFrameNumber:(int)fn {
//	NSLog(@"Setting FN to %i", fn );
	frameNumber = fn;
}

@end
